from path import path as Path
from collections import deque

__all__ = ["SQLFileParser"]

class SQLFileParser(object):
    """
    This is a class to parse an SQL file from a text file. What we shall do 
    is to iterate through all the lines and just execute multi-line strings 
    as a single single line syntax split by a delimiter. This just makes it 
    easier to define tables etc.
    """
    staticDelimiter = ";" # The delimiter which the parser will use natively
    
    def __init__(self, pathFile):
        """
        This constructor assigns the default values
        
        @param str|path.path pathFile The path to the file which we wish
                                      to parse. 
        """
        if not isinstance(pathFile, Path):
            self.pathFile = Path(pathFile)
        else:
            self.pathFile = pathFile
        
        self.commands = deque()
    
    def read(self):
        """
        This function will loop through the file and store all the commands
        in the commands variable. We test for an end of command by the
        delimiter.
        """
        fileStream = self.pathFile.open("r")
        
        """ Loop through the file """
        currentCommand = ""
        for line in fileStream:
            line = line.strip()
            currentCommand += (line + " ")
            if line.endswith(self.__class__.staticDelimiter):
                self.commands.append(currentCommand[:-1])
                currentCommand = ""
        
        """ Close the file """
        fileStream.close()
    
    def __iter__(self):
        """
        Return the iterable generator from the commands deque.
        
        @return generator of all the commands.
        """
        return self.commands.__iter__()